package user.zc.controller.impl;

import user.zc.config.user.config.ConnectionConfig;
import user.zc.config.value.KeyValue;
import user.zc.controller.IConnect;
import user.zc.service.ConnectService;
import user.zc.vo.ConnectionDTO;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhouchuang
 * @date 2023/8/8 17:25
 */
public class ConnectCollector implements IConnect {


    private ConnectService connectService = new ConnectService();

    @Override
    public List<String> connectList()throws Exception  {
        return connectService.connectList();
    }

    @Override
    public Boolean switchConnections(String connection)throws Exception {
        connectService.switchConnections(connection);
        return connectService.validationQuery();
    }

    @Override
    public void saveOrUpdate(String connection, ConnectionDTO connectionDTO)throws Exception  {
        KeyValue urlValue = new KeyValue("url",connectionDTO.getUrl());
        KeyValue userValue = new KeyValue("user",connectionDTO.getUser());
        KeyValue passValue = new KeyValue("password",connectionDTO.getPassword());
        Map<String,KeyValue> map = new LinkedHashMap<>();
        map.put(urlValue.getKey(),urlValue);
        map.put(userValue.getKey(),userValue);
        map.put(passValue.getKey(),passValue);
        connectService.saveOrUpdate(connection,map);
    }

    @Override
    public ConnectionDTO read()throws Exception  {
        ConnectionConfig connCfg  = connectService.read();
        return new ConnectionDTO(connCfg.getUrl(),connCfg.getUser(),connCfg.getPassword());
    }

    @Override
    public ConnectionDTO read(String connection) throws Exception {
        Map<String, KeyValue> read = connectService.read(connection);
        return new ConnectionDTO(read.get("url").getValue(),read.get("user").getValue(),read.get("password").getValue());
    }

    @Override
    public void deleteConection(String name) throws Exception{
        connectService.deleteConection(name);
    }

}
